#!/usr/bin/env bash

set -eu
script_name=$0

die() {
    echo >&2 "$@"
    exit 1
}

about() {
    die "usage: ${script_name} [make | load | clean]"
}

#shellcheck disable=SC1007
THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
cd "${THIS_DIR}"/../../
#shellcheck disable=SC1091
. ./.environment.sh

# you have not removed set -u above, have you?

[[ -z "${TEST_DIR-}" ]] && die "\$TEST_DIR must be defined."
[[ -z "${LOCAL_DIR-}" ]] && die "\$LOCAL_DIR must be defined."
[[ -z "${CSCLI-}" ]] && die "\$CSCLI must be defined."
[[ -z "${LOCAL_INIT_DIR-}" ]] && die "\$LOCAL_INIT_DIR must be defined."
[[ -z "${PLUGIN_DIR-}" ]] && die "\$PLUGIN_DIR must be defined."
[[ -z "${DB_BACKEND-}" ]] && die "\$DB_BACKEND must be defined."

if [[ ! -f "${CSCLI}" ]]; then
    die "${CSCLI} is missing. Please build (with 'make bats-build') or install it."
fi

REL_CONFIG_DIR="etc/crowdsec"
REL_DATA_DIR="var/lib/crowdsec/data"

DATA_DIR="${LOCAL_DIR}/${REL_DATA_DIR}"
export DATA_DIR
CONFIG_DIR="${LOCAL_DIR}/${REL_CONFIG_DIR}"
export CONFIG_DIR
HUB_DIR="${CONFIG_DIR}/hub"
export HUB_DIR

if [[ $(uname) == "OpenBSD" ]]; then
    TAR=gtar
else
    TAR=tar
fi

remove_init_data() {
    ./bin/assert-crowdsec-not-running || die "Cannot remove fixture data."
    rm -rf -- "${LOCAL_DIR:?}/${REL_CONFIG_DIR}"/* "${LOCAL_DIR:?}/${REL_DATA_DIR:?}"/*
}

# we need a separate function for initializing config when testing package
# because we want to test the configuration as well
config_prepare() {
    # remove trailing slash from CONFIG_DIR
    # since it's assumed to be missing during the tests
    yq e -i '
        .api.server.listen_socket="/run/crowdsec.sock" |
        .config_paths.config_dir |= sub("/$", "")
    ' "${CONFIG_DIR}/config.yaml"

    # pin the branch to avoid having to query the last version repeatedly.
    # this means the fixture could possibly go stale (i.e. use the wrong branch) if a new version is released,
    # but that shouldn't impact the tests anyway.

    HUB_BRANCH=$("$CSCLI" hub branch 2>/dev/null)
    export HUB_BRANCH

    echo "Setting up tests with hub branch $HUB_BRANCH"

    # need a working config, so we do it as a separate step.

    yq -i e '.cscli.hub_branch=strenv(HUB_BRANCH)' "${CONFIG_DIR}/config.yaml"
}

make_init_data() {
    ./bin/assert-crowdsec-not-running || die "Cannot create fixture data."

    config_prepare
    ./instance-db config-yaml
    ./instance-db setup

    # preload some content and data files
    "$CSCLI" collections install crowdsecurity/linux --download-only
    # sub-items did not respect --download-only
    ./bin/remove-all-hub-items

    # when installed packages are always using sqlite, so no need to regenerate
    # local credz for sqlite

    [[ "${DB_BACKEND}" == "sqlite" ]] || ${CSCLI} machines add githubciXXXXXXXXXXXXXXXXXXXXXXXX --auto --force

    mkdir -p "$LOCAL_INIT_DIR"

    ./instance-db dump "${LOCAL_INIT_DIR}/database"

    echo "${DB_BACKEND}" > "${LOCAL_INIT_DIR}/.backend"

    # disable CAPI by default
    yq e 'del(.api.server.online_client)' -i "${CONFIG_DIR}/config.yaml"

    "${TAR}" -C "${LOCAL_DIR}" --create \
        --exclude "${REL_DATA_DIR}"/crowdsec.db \
        --file "${LOCAL_INIT_DIR}/init-config-data.tar" "${REL_CONFIG_DIR}" "${REL_DATA_DIR}"
}

load_init_data() {
    ./bin/assert-crowdsec-not-running || die "Cannot load fixture data."

    if [[ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]]; then
        die "Initial data not found; did you run 'make bats-fixture' ?"
    fi

    dump_backend="$(cat "${LOCAL_INIT_DIR}/.backend")"
    if [[ "${DB_BACKEND}" != "${dump_backend}" ]]; then
        die "Can't run with backend '${DB_BACKEND}' because 'make bats-fixture' was ran with '${dump_backend}'"
    fi

    remove_init_data

    "${TAR}" -C "${LOCAL_DIR}" --extract --file "${LOCAL_INIT_DIR}/init-config-data.tar"

    ./instance-db restore "${LOCAL_INIT_DIR}/database"
}

# ---------------------------

[[ $# -lt 1 ]] && about

case "$1" in
    make)
        "${TEST_DIR}/instance-crowdsec" stop
        make_init_data
        ;;
    load)
        load_init_data
        ;;
    clean)
        remove_init_data
        ;;
    *)
        about
        ;;
esac;

